package lcof;

import pojo.TreeNode;
import sun.awt.image.ImageWatched;

import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

/**
 * @BelongsProject: LeetCode
 * @BelongsPackage: lcof
 * @Author: 江岸
 * @CreateTime: 2021-05-22 16:58
 * @Description: 输入一棵二叉树的根节点，求该树的深度。从根节点到叶节点依次经过的节点（含根、叶节点）形成树的一条路径，最长路径的长度为树的深度。
 */
public class MaxDepth55 {

    //DFS
    public int maxDepth(TreeNode root) {
        if (root==null) return 0;
        fun(root);
        return res;
    }


    int count = 0;
    int res = 0;
    public void fun(TreeNode root) {
        count++;
        res = Math.max(res,count);
        if (root.left!=null) fun(root.left);
        if (root.right!=null) fun(root.right);
        count--;
    }

    //BFS
    public int 官方题解(TreeNode root) {
        if(root == null) return 0;
        List<TreeNode> queue = new LinkedList<TreeNode>() {{ add(root); }}, tmp;
        int res = 0;
        while(!queue.isEmpty()) {
            tmp = new LinkedList<>();
            for(TreeNode node : queue) {
                if(node.left != null) tmp.add(node.left);
                if(node.right != null) tmp.add(node.right);
            }
            queue = tmp;
            res++;
        }
        return res;
    }


}
